﻿//  Copyright © Decebal Mihailescu 2013-2014
//  All rights reserved. This source code and binaries are distributed under 
//  Microsoft Public PatternLicense (Ms-PL)
//  The latest code is available at http://VSDesignPatterns.codeplex.com

//  Redistributions of source code must retain the above copyright notice, 
//  this list of conditions and the following disclaimer. 
//  Redistributions in binary form must reproduce the above copyright notice, 
//  this list of conditions and the following disclaimer in the documentation 
//  and/or other materials provided with the distribution. 

//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER 
//  REMAINS UNCHANGED.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using System.Globalization;

namespace DesignPatternsWizard
{
    using Common;
    public partial class FormAbout : Form, IAboutWnd 
    {
        public FormAbout()
        {
            InitializeComponent();

        }
        float _assemblyVer;
        private void _buttonCheck4NewVersion_Click(object sender, EventArgs e)
        {
            try
            {

                this.UseWaitCursor = true;
                Application.DoEvents();
                string downloadUrl;
                float onlineVersion;
                Utils.LastOnlineCheck = DateTime.Today;
                if (Utils.NewOnlineVersionExists(out downloadUrl, out onlineVersion))
                {
                    if (MessageBox.Show(this, "Version " + onlineVersion.ToString(CultureInfo.InvariantCulture) + " is available online.\r\nWould you like to download it?", "A new version is available", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(string.IsNullOrEmpty(downloadUrl) ? Utils.BaseURL : downloadUrl);
                    }
                }
                else
                    if (onlineVersion > _assemblyVer || onlineVersion == 0.0)//not sure
                        System.Diagnostics.Process.Start(Utils.BaseURL);
                    else
                        MessageBox.Show(this, "There is no new version available online.\r\nCheck back in a few days.", "No new version is available");

            }
            catch //(Exception ex)
            {

            }
            finally
            {
                UseWaitCursor = false;
            }
        }

        private void _buttonDonate_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3UNEYK64HSWTJ");
        }

        private void FormAbout_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
            try
            {
                IWizardWindow iww = this.Owner as IWizardWindow;
                IProjectInfo ipi = iww.GetAsService<IProjectInfo>();
                Assembly targetAssembly = ipi.GetType().Assembly;
                Version v = targetAssembly.GetName().Version;
                _assemblyVer = float.Parse(string.Format("{0}.{1}", v.Major, v.Minor), System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
                _textBoxInfo.Text += string.Format(" Assembly Version:{0}", _assemblyVer.ToString(CultureInfo.InvariantCulture));

                var attrFv = targetAssembly.GetAssemblyAttribute<AssemblyFileVersionAttribute>();
                if (attrFv != null)
                    _textBoxInfo.Text += string.Format("{0} Assembly FileVersion:{1}",
                        Environment.NewLine, attrFv.Version);

                var attrTitle = targetAssembly.GetAssemblyAttribute<AssemblyTitleAttribute>();
                if (attrTitle != null)
                    _textBoxInfo.Text += string.Format("{0} Assembly Title:{1}", Environment.NewLine, attrTitle.Title);
                var attrDesc = targetAssembly.GetAssemblyAttribute<AssemblyDescriptionAttribute>();
                if (attrDesc != null)
                    _textBoxInfo.Text += string.Format("{0} Assembly Description:{1}",
                        Environment.NewLine, attrDesc.Description);
                var attrCopy = targetAssembly.GetAssemblyAttribute<AssemblyCopyrightAttribute>();
                if (attrCopy != null)
                    _textBoxInfo.Text += string.Format("{0} Assembly Copyright:{1}",
                        Environment.NewLine, attrCopy.Copyright);
                var attrComp = targetAssembly.GetAssemblyAttribute<AssemblyCompanyAttribute>();
                if (attrComp != null)
                    _textBoxInfo.Text += string.Format("{0} Assembly Company:{1}",
                        Environment.NewLine, attrComp.Company);
                var attrTrade = targetAssembly.GetAssemblyAttribute<AssemblyTrademarkAttribute>();
                if (attrTrade != null)
                    _textBoxInfo.Text += string.Format("{0} Assembly Trademark:{1}",
                        Environment.NewLine, attrTrade.Trademark);
                    _textBoxInfo.Text += string.Format("{0} The License is available at {1}",
                        Environment.NewLine, "http://vsdesignpatterns.codeplex.com/license");

                _linkHomePage.Links.Clear();
                _linkTutorial.Links.Clear();
               
                _linkHomePage.Links.Add(13,9,
                    "\"http://vsdesignpatterns.codeplex.com/\"");
                _linkTutorial.Links.Add(7,9,
                    "\"http://www.youtube.com/watch?v=S4Ni_5ay8_M\"");

            }
            catch (InvalidOperationException)
            {
                
                
            }
        }

        private void _linkHomePage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = (sender as LinkLabel).Links[0].LinkData.ToString();
            p.Start();
        }

    }
}
